package com.example.endproject;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

public class ManagerUpdateActivity extends Activity {
    private static final String TAG = "ManagerUpdateActivity";
    String tempUserName,tempID;
    String userName,pwd;//用户名,密码
    String ID,name,age,department,hobby,constellation,signature;//个人信息
    TextView userNameView,pwdView,IDView,nameView,ageView,departmentView,hobbyView,constellationView,signatureView;//控件
    DBHelper dbHelper;
    SQLiteDatabase mSqlDB;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_manager_update);
        //设置字体
        TextView signSubjectView,personalSubjectView;
        signSubjectView = findViewById(R.id.textView5);
        personalSubjectView = findViewById(R.id.textView9);
        signSubjectView.setTypeface(Typeface.createFromAsset(getAssets(),"FZBangSXJW.ttf"));
        personalSubjectView.setTypeface(Typeface.createFromAsset(getAssets(),"FZBangSXJW.ttf"));
        //初始化控件
        IDView = findViewById(R.id.update_ID);
        nameView = findViewById(R.id.update_name);
        ageView = findViewById(R.id.update_age);
        departmentView = findViewById(R.id.update_depart);
        hobbyView = findViewById(R.id.update_hobby);
        constellationView = findViewById(R.id.update_constellation);
        signatureView = findViewById(R.id.update_signature);
        userNameView = findViewById(R.id.update_userName);
        pwdView = findViewById(R.id.update_pwd);
        //初始化数据库
        dbHelper = new DBHelper(this);
        mSqlDB = dbHelper.getWritableDatabase();
        //获得传过来的值
        Intent intent = getIntent();
        tempUserName= userName = intent.getStringExtra("user_name");
        pwd = intent.getStringExtra("pwd");
        tempID = ID = intent.getStringExtra("ID");
        
        //查询数据
        Cursor query = mSqlDB.query(DBHelper.TB_NAME_MSG,
                new String[]{"ID", "name", "age", "department", "hobby", "constellation", "signature"},
                "ID = ?", new String[]{ID}, null, null, null, null);
        if(!query.moveToFirst()){
            Log.i(TAG, "onCreate: 没有查询到相应数据");
            return;
        }
        //写入成员变量
        name = query.getString(query.getColumnIndexOrThrow("name"));
        age = query.getString(query.getColumnIndexOrThrow("age"));
        department = query.getString(query.getColumnIndexOrThrow("department"));
        hobby = query.getString(query.getColumnIndexOrThrow("hobby"));
        constellation = query.getString(query.getColumnIndexOrThrow("constellation"));
        signature = query.getString(query.getColumnIndexOrThrow("signature"));
        //写入控件
        userNameView.setText(userName);
        pwdView.setText(pwd);
        IDView.setText(ID);
        nameView.setText(name);
        ageView.setText(age);
        departmentView.setText(department);
        hobbyView.setText(hobby);
        constellationView.setText(constellation);
        signatureView.setText(signature);
    }

    public void onclick_update(View view){
        //获取控件中的值
        userName = userNameView.getText().toString();
        pwd = pwdView.getText().toString();
        ID = IDView.getText().toString();
        name = nameView.getText().toString();
        age = ageView.getText().toString();
        department = departmentView.getText().toString();
        hobby = hobbyView.getText().toString();
        constellation = constellationView.getText().toString();
        signature = signatureView.getText().toString();
        try{
            //开始事物
            mSqlDB.beginTransaction();
            //更新用户表
            ContentValues values = new ContentValues();
            values.put("user_name",userName);
            values.put("pwd",pwd);
            if (!userName.equals(tempUserName)) {//用户名未改动,直接更新用户表
                //用户名改动了,必须判断用户表中是否有相同的用户名,有的话则提示
                Cursor query = mSqlDB.query(DBHelper.TB_NAME_USER, new String[]{"user_name", "pwd"}, "user_name = ?",
                        new String[]{userName}, null, null, null, null);
                if (query.moveToFirst()) {//存在相同的值
                    userNameView.setHint("存在相同用户名");
                    userNameView.setText("");
                    query.close();
                    return;
                }
                query.close();
                //不存在则更新
            }
            mSqlDB.update(DBHelper.TB_NAME_USER,values,"user_name = ?",new String[]{tempUserName});
            //更新信息表
            ContentValues values1 = new ContentValues();
            values1.put("ID",ID);
            values1.put("name",name);
            values1.put("age",age);
            values1.put("department",department);
            values1.put("hobby",hobby);
            values1.put("constellation",constellation);
            values1.put("signature",signature);
            if(!ID.equals(tempID)){//ID未改动,直接更新信息表
                //ID改动了,必须判断信息表中是否有相同的ID,有的话则提示
                Cursor query2 = mSqlDB.query(DBHelper.TB_NAME_MSG,
                        new String[]{"ID", "name", "age", "department", "hobby", "constellation", "signature"}
                        , "ID = ?", new String[]{ID}, null, null, null);
                if (query2.moveToFirst()){//有相同的ID
                    IDView.setHint("存在相同的ID名");
                    IDView.setText("");
                    query2.close();
                    return;
                }
                query2.close();
            }
            mSqlDB.update(DBHelper.TB_NAME_MSG,values1,"ID = ?",new String[]{tempID});
            //更新关系表
            ContentValues values2 = new ContentValues();
            values2.put("user_name",userName);
            values2.put("ID",ID);
            mSqlDB.update(DBHelper.TB_NAME_Relate,values2,"user_name = ?",new String[]{tempUserName});
            mSqlDB.setTransactionSuccessful();
        }catch (SQLException e){
            Log.i(TAG, "onclick_update: 管理员修改出错辣");
        }
        finally {
            mSqlDB.endTransaction();
        }
        Intent intent = getIntent();
        intent.putExtra("user_name2",userName);
        intent.putExtra("pwd2",pwd);
        intent.putExtra("ID2",ID);
        intent.putExtra("name2",name);
        setResult(2000,intent);
        finish();
    }
}